﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace OpenSoftStore.Client.UIL.UserControls
{
    /// <summary>
    /// 对象名称：自定义的TextBox控件
    /// 对象说明：相对于原生的TextBox控件，具备了更好的UI显示效果。
    /// 作者姓名：
    /// 编写日期：
    /// </summary>
    [DefaultProperty("Text")]
    public partial class TextBox : UserControl
    {
        private string _prompt = "";

        public event EventHandler TextChanged;
        /// <summary>
        /// 自定义TextBox控件的默认实例化方法。使用本自定义Button控件前，请确保
        /// 应用程序的Properties.Resources资源文件中，包含了1张名为Input的图片。
        /// </summary>
        public TextBox()
        {
            InitializeComponent();
            TxtInside.BackColor = Color.White;
            TxtInside.Location = new Point(3, 6);
            TxtInside.Width = PnlWhiteBG.Width - 6;
            TxtInside.GotFocus += new EventHandler(TxtBox_GotFocus);
            TxtInside.Leave += new EventHandler(TxtBox_Leave);
            TxtInside.KeyDown += new KeyEventHandler(KeyDowns);
            TxtInside.KeyPress += new KeyPressEventHandler(KeyPresss);
            TxtInside.KeyUp += new KeyEventHandler(KeyUps);
            TxtInside.Enter += new EventHandler(Enters);
            TxtInside.Leave += new EventHandler(Leaves);
            this.TextChanged += TxtChanged;
        }
        private void TxtChanged(object sender, EventArgs e)
        {
            this.OnTextChanged(e);
        }

        private void Leaves(object sender, EventArgs e)
        {
            this.OnLeave(e);
        }

        private void Enters(object sender, EventArgs e)
        {
            BackColor = Color.FromArgb(179, 221, 150);
            PnlBorder.BackColor = Color.FromArgb(87, 152, 43);
            TxtInside.ForeColor = Color.FromArgb(51, 102, 0);
            TxtInside.SelectAll();
            this.OnEnter(e);
        }
        public void KeyUps(object sender, KeyEventArgs e)
        {
            var findForm = FindForm();
            if (findForm == null) return;
            if (e.KeyCode == Keys.Enter)
            {
                findForm.SelectNextControl(this, true, true, true, true);
            }
            this.OnKeyUp(e);
        }
        public void KeyPresss(object sender, KeyPressEventArgs e)
        {
            this.OnKeyPress(e);
        }
        public void KeyDowns(object sender, KeyEventArgs e)
        {
            this.OnKeyDown(e);
        }

        /// <summary>
        /// 获取或设置文本框中的内容。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框中的内容。")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ReadOnly
        {
            get
            {
                return TxtInside.ReadOnly;
            }
            set
            {
                TxtInside.ReadOnly = value;
                if (TxtInside.ReadOnly)
                {
                    TxtInside.BackColor = Color.WhiteSmoke;
                    PnlWhiteBG.BackColor = Color.WhiteSmoke;
                    PnlImageBG.BackColor = Color.WhiteSmoke;
                }
                else
                {
                    TxtInside.BackColor = Color.White;
                    PnlWhiteBG.BackColor = Color.White;
                    PnlImageBG.BackColor = Color.White;
                }
            }
        }

        /// <summary>
        /// 获取或设置文本框中的内容。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框中的内容。")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return TxtInside.Text == Prompt ? "" : TxtInside.Text;
            }
            set
            {
                this.ForeColor = Color.Black;
                TxtInside.ForeColor = Color.FromArgb(102, 102, 102);
                TxtInside.Text = value;
            }
        }

        /// <summary>
        /// 获取或设置文本框中的内容。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框中的内容。")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string Prompt
        {
            get
            {
                return _prompt;
            }
            set
            {
                _prompt = value;
                TxtInside.Text = _prompt;
                if (!string.IsNullOrEmpty(value)) TxtInside.ForeColor = Color.LightGray;
                else TxtInside.ForeColor = Color.Black;
            }
        }

        /// <summary>
        /// 获取或设置是否为多行文本框。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置是否为多行文本框。")]
        [Browsable(true)]
        public bool Multiline
        {
            get
            {
                return TxtInside.Multiline;
            }
            set
            {
                TxtInside.Multiline = value;

            }
        }

        /// <summary>
        /// 获取或设置文本框的滚动条显示方式。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置文本框的滚动条显示方式。")]
        [Browsable(true)]
        public ScrollBars ScrollBars
        {
            get
            {
                return TxtInside.ScrollBars;
            }
            set
            {
                TxtInside.ScrollBars = value;

            }
        }

        /// <summary>
        /// 获取或设置密码字符。
        /// </summary>
        [Category("设置")]
        [Description("获取或设置密码字符。")]
        [Browsable(true)]
        public char PasswordChar
        {
            get
            {
                return TxtInside.PasswordChar;
            }
            set
            {
                TxtInside.PasswordChar = value;

            }
        }

        /// <summary>
        /// 输入焦点丢失时，改变相关子控件的颜色。
        /// </summary>
        void TxtBox_Leave(object sender, EventArgs e)
        {
            BackColor = Color.White;
            PnlBorder.BackColor = Color.FromArgb(204, 204, 204);
            TxtInside.ForeColor = Color.FromArgb(102, 102, 102);

            if (string.IsNullOrEmpty(TxtInside.Text))
            {
                TxtInside.Text = _prompt;
                TxtInside.ForeColor = Color.LightGray;
                //else TxtInside.ForeColor = Color.Black;
            }
        }

        /// <summary>
        /// 获得输入焦点时，改变相关子控件的颜色。
        /// </summary>
        void TxtBox_GotFocus(object sender, EventArgs e)
        {
            BackColor = Color.FromArgb(179, 221, 150);
            PnlBorder.BackColor = Color.FromArgb(87, 152, 43);
            TxtInside.ForeColor = Color.FromArgb(51, 102, 0);
            this.ForeColor = Color.Black;
            if (!string.IsNullOrWhiteSpace(_prompt) && TxtInside.Text == _prompt)
            {
                TxtInside.Text = "";
                //_prompt = "";
            }
            TxtInside.SelectAll();
        }

        /// <summary>
        /// 可以手动通过该方法，使TextBox获得输入焦点。
        /// </summary>
        public new void Focus()
        {
            TxtInside.Select();
            //TxtInside.SelectAll();
        }

        /// <summary>
        /// TextBox控件大小改变时的事件处理。
        /// </summary>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (!Multiline)
            {
                //this.Height = 21;
            }
            TxtInside.Location = new Point(3, 5);
            TxtInside.Width = PnlWhiteBG.Width - 6;
            TxtInside.Height = PnlWhiteBG.Height - 10;
        }


    }
}
